#include "SudokuLockedCandidatesClaiming.h"

SudokuLockedCandidatesClaiming::SudokuLockedCandidatesClaiming(QObject* parent) :
        SudokuAbstractSolvingTechnique("LC Claiming",
                                       LockedCandidatesClaiming, EliminateCandidate, parent)
{
}

int SudokuLockedCandidatesClaiming::solve()
{
    //TODO test that method!!!

    for(int number=0; number<mSudokuData->getSize(); ++number)
    {
        for(int lineIndex=0; lineIndex<mSudokuData->getSize(); ++lineIndex)
        {
            QBitArray colCount(mSudokuData->getSizeBox());
            QBitArray rowCount(mSudokuData->getSizeBox());
            const SudokuHouse* row = mSudokuData->houseAtRow(lineIndex);
            const SudokuHouse* col = mSudokuData->houseAtCol(lineIndex);
            bool rowContainsValue = false;
            bool colContainsValue = false;

            for(int cellInd=0; cellInd<mSudokuData->getSize(); ++cellInd)
            {
                SudokuCell* cellRow = row->at(cellInd);//TODO change to []
                if(!cellRow->isEmpty())
                {
                    if(cellRow->getValue() == number+1)
                    {
                        qDebug() << "SudokuData::findLockedCandidatesClaiming break";
                        rowContainsValue = true;
                        cellInd = mSudokuData->getSize();//just to break loop
                    }
                }
                else if(cellRow->getPossibl(number))
                    rowCount.setBit(cellRow->getTowerIndex(), true);
                //Use unknown as it is not usual use and could set again
            }

            //TODO change 2 for to one!
            for(int cellInd=0; cellInd<mSudokuData->getSize(); ++cellInd)
            {
                SudokuCell* cellCol = col->at(cellInd);//TODO change to []
                if(!cellCol->isEmpty())
                {
                    if(cellCol->getValue() == number+1)
                    {
                        qDebug() << "SudokuData::findLockedCandidatesClaiming break";
                        colContainsValue = true;
                        cellInd = mSudokuData->getSize();//just to break loop
                    }
                }
                else if(cellCol->getPossibl(number))
                    colCount.setBit(cellCol->getFloorIndex(), true);
                //Use unknown as it is not usual use and could set again
            }

            if(!colContainsValue && colCount.count(true) == 1)
            {
                SudokuHouse* house = mSudokuData->houseAtBox(lineIndex/mSudokuData->getSizeBox() + getFirstTrueIndex(colCount)*mSudokuData->getSizeBox());
                mCount += house->eliminate(number,LockedCandidatesClaiming,lineIndex,-1,-1);
            }

            if(!rowContainsValue && rowCount.count(true) == 1)
            {
                SudokuHouse* house = mSudokuData->houseAtBox((lineIndex/mSudokuData->getSizeBox())*mSudokuData->getSizeBox() + getFirstTrueIndex(rowCount));
                mCount += house->eliminate(number,LockedCandidatesClaiming,-1,lineIndex,-1);
            }

        }
    }

    return mCount;
}


